<?php

Class Login extends CI_Controller {

//constructor
    function __construct() {
        parent::__construct();
    }
    
    function redirect(){
        if (isset($this->session->userdata['type'])) {
            if ($this->session->userdata['type'] == 'admin') {
                redirect('admin/admin');
            } else if ($this->session->userdata['type'] == 'guru') {
                redirect('guru');
            } else if ($this->session->userdata['type'] == 'siswa') {
                redirect('siswa');
            } else {
                $this->load->view('login_view');
            }
        } else {
            $this->load->view('login_view');
        }
    }

//index for showing the login form
    function index() {
//        $this->load->view('login_view');
        $this->redirect();
    }

//this function will do the login process
    function proseslogin() {
        $this->load->model('model_user');
        
        if($this->model_user->validate()){
            if($this->session->userdata['type'] == 'admin'){
                redirect('admin/admin');
            }else if($this->session->userdata['type'] == 'guru'){
                redirect('guru/index');
                //view guru
            }else if($this->session->userdata['type'] == 'siswa'){
                redirect('siswa/index');
                //view siswa
            }
        }else{
            $data['error'] = 'Anda tidak terdaftar.';
            $this->load->view('login_view', $data);
        }
    }

//to do logout process
    function logout() {
        $this->session->sess_destroy();
        $data['logout'] = 'You have been logged out.';
        $this->load->view('login_view', $data);
    }
    

}

?>